package Dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Dao.Daouser;
import entity.User;
import utils.BaseDao;

public class Daoimpluser extends BaseDao implements Daouser {

	@Override
	public boolean add(String yhm, String mima) {
		String sql = "insert into `xiangmu`.`user` (`phone`, `password`, `uid`) values (?, ?, 1); ";
		Object[] object = {yhm,mima};
		return super.executeUpdate(sql, object);
	}

	@Override
	public User sel(String yhm, String mima) {
		String sql = "select * from user where phone = ? and password=?";
		Object[] object = {yhm,mima};
		ResultSet set = super.executeQuery(sql, object);
		User user = null;
		try {
			while(set.next()){
				user = new User();
				user.setId(set.getInt(1));
				user.setName(set.getString(2));
				user.setPassword(set.getString(3));
				user.setUid(set.getInt(4));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	@Override
	public User currentU(int id) {
		String sql="select * from `user` where `id`=?";
		Object[] cmh={id};
		ResultSet set = super.executeQuery(sql, cmh);
		User user = null;
		try {
			while(set.next()){
				user = new User();
				user.setId(set.getInt(1));
				user.setName(set.getString(2));
				user.setPassword(set.getString(3));
				user.setUid(set.getInt(4));
				user.setPhone(set.getString(5));
				user.setImg(set.getString(6));
				user.setDate(set.getTimestamp(7).toString());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	@Override
	public boolean updName(User u) {
		String sql="update `xiangmu`.`user` set `name` = ? where `id` = ? ";
		Object[] cmh={u.getName(),u.getId()};
		return super.executeUpdate(sql, cmh);
	}

	@Override
	public boolean updPwd(User u) {
		String sql="update `xiangmu`.`user` set `password` = ? where `id` =?";
		Object[] cmh={u.getPassword(),u.getId()};
		return super.executeUpdate(sql, cmh);
	}

	@Override
	public List<User> selhou() {
		String sql = "select * from user";
		ResultSet set = super.executeQuery(sql, null);
		List<User> list = new ArrayList<User>();
		try {
			while(set.next()){
				User u = new User();
				u.setId(set.getInt(1));
				u.setName(set.getString(2));
				u.setPassword(set.getString(3));
				u.setUid(set.getInt(4));
				u.setPhone(set.getString(5));
				u.setImg(set.getString(6));
				u.setDate(set.getTimestamp(7).toString());
				list.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean updimg(String tu,int id) {
		String sql = "update `xiangmu`.`user` set `img` = ? where `id` =?";
		Object[] object = {tu,id};
		return super.executeUpdate(sql,object);
	}

	@Override
	public int houadd(String name, String password) {
		String sql = "select count(*) from userhou where name = ? and password = ?";
		Object[] object = {name,password};
		ResultSet set = super.executeQuery(sql, object);
		int fan = 0;
		try {
			while(set.next()){
				fan=set.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(set, set.getStatement(), set.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return fan;
	}

}
